# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import pymysql
from itemadapter import ItemAdapter


class MyappPipeline:
    def process_item(self, item, spider):
        # print('MyappPipeline')
        # print(item)
        self.save_data(item['info'])
        return item

    def save_data(self, data):
        conn = pymysql.connect(host="localhost", user="root",
                               password="1234", db="b-video-stat", port=3306)
        cur = conn.cursor()

        sql = """
        INSERT INTO `b-video-stat`.`ranks`
        (`score`, `url`, `type`, `aid`, `pic`, `title`, `desc`, `mid`, `name`, `face`, `view`, `danmaku`, `reply`, `favorite`, `coin`, `share`, `like`, `cid`, `follower`) 
        VALUES 
        ({})
        """.format(",".join(data))

        row = cur.execute(sql)
        if row == 1:
            print("插入成功！")
        else:
            print(row)
        conn.commit()

        cur.close()
        conn.close()

